/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "generalsetting.h"

CGeneralSetting::CGeneralSetting(QObject *parent) : QObject(parent),
    myState("none")
{

}

void CGeneralSetting::setCenterState(const QString &centerState)
{
    qDebug() << "CGeneralSetting::setCenterState" << centerState;
    if(myState != centerState){
        qDebug() << "emit centerStateChanged";
        myState = centerState;
        emit centerStateChanged(myState);
    }
}

void CGeneralSetting::openOnboard()
{
    if (!QDBusConnection::sessionBus().isConnected()) {
        qDebug()<<"Cannot connect to the D-Bus session bus.";
    }

    QDBusInterface iface("org.onboard.Onboard",
                         "/org/onboard/Onboard/Keyboard",
                         "org.onboard.Onboard.Keyboard",
                         QDBusConnection::sessionBus());

    if (iface.isValid())
    {
        iface.call("Show");
    }

}

void CGeneralSetting::openControlCenter(const QString &controlStr)
{
    qDebug() << "CGeneralSetting::openControlCenter()" << controlStr;
    controlCenter.runApplication(controlStr);
}

void CGeneralSetting::openControlCenterConfig(const QString &controlStr, const QStringList &config)
{
    qDebug() << "CGeneralSetting::openControlCenterConfig()" << controlStr << " " << config;
    controlCenter.runApplicationConfig(controlStr, config);
}

QString CGeneralSetting::getCenterState() const
{
    qDebug() << "CGeneralSetting::getCenterState()";
    return myState;
}

void CGeneralSetting::statePrint()
{
    qDebug() << "CGeneralSetting::statePrint()";
}

void CGeneralSetting::openGeneralSetting(const QString &str)
{
    qDebug() << "CGeneralSetting::openGeneralSetting" << str;
    if(str == "onboard"){
        openOnboard();
    }
    else if(str == "ukui-control-center.desktop"){
        openControlCenter(str);
    }
    else if(str == "ukui-control-center-u"){
        openControlCenterConfig("ukui-control-center.desktop",{"-m","userinfo"});
    }
}
